package com.ybk.zs.controller;

import com.ybk.zs.common.utils.LoggerUtil;
import com.ybk.zs.common.utils.StringUtils;
import com.ybk.zs.controller.resp.BaseResp;
import com.ybk.zs.controller.resp.QueryAllCustomerRsp;
import com.ybk.zs.domain.Customer;
import com.ybk.zs.domain.CustomerExample;
import com.ybk.zs.domain.ex.CostScoreEx;
import com.ybk.zs.domain.ex.ScoreCostEx;
import com.ybk.zs.service.ICostScoreExService;
import com.ybk.zs.service.ICustomerService;
import com.ybk.zs.service.IScoreCostExService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by duanjixing.
 */
@Controller
@RequestMapping(value = "/customer/")
public class CustomerController {

    @Autowired
    ICustomerService customerService;

    @Autowired
    ICostScoreExService costScoreExService;

    @Autowired
    IScoreCostExService scoreCostExService;

    @RequestMapping(value = "queryAllCustomer", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public QueryAllCustomerRsp queryAllCustomer() {

        LoggerUtil.info("CustomerController", "queryAllCustomer", "初始化会员页，查找所有的会员");
        QueryAllCustomerRsp resp = new QueryAllCustomerRsp();

        try {
            CustomerExample example = new CustomerExample();
            CustomerExample.Criteria criteria= example.createCriteria();
            example.setOrderByClause("score desc");
            List<Customer> list = customerService.selectByExample(example);
            if (list != null && list.size() > 0) {
                resp.setCustomerList(list);
            }
//            if (list != null && list.size() > 0) {
//                //获取会员积分
//                List<CostScoreEx> list1 = costScoreExService.getCustomerScore(0);
//                //获取会员已经消费的积分
//                List<ScoreCostEx> list2 = scoreCostExService.getCustomerCostScore(0);
//
//                for(CostScoreEx costScoreEx: list1){
//                    for(Customer customer:list){
//                        if(customer.getNum().equals(costScoreEx.getCostomerNum())){
//                            customer.setScore(costScoreEx.getCustomerScore());
//                            break;
//                        }
//                    }
//                }
//
//                for(ScoreCostEx scoreCostEx:list2){
//                    for(Customer customer:list){
//                        if(customer.getNum().equals(scoreCostEx.getCostomerNum())){
//                            customer.setScore(customer.getScore()-scoreCostEx.getCustomerScore());
//                            break;
//                        }
//                    }
//                }
//
//                resp.setCustomerList(list);
//            }

            resp.setResultCode("001");
            resp.setResultMsg("处理成功");
        } catch (Exception e) {
            LoggerUtil.error(e, "查询全部会员出现异常");
            resp.setResultCode("003");
            resp.setResultMsg("query all customer exception");
        }
        return resp;
    }

    @RequestMapping(value = "deleteCustomerByNum", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public BaseResp deleteCustomerByNum(Integer num) {
        LoggerUtil.info("CustomerController", "deleteCustomerByNum", "delete customer by num");

        BaseResp resp = new BaseResp();

        try {
            CustomerExample example = new CustomerExample();
            example.or().andNumEqualTo(num);
            customerService.deleteByExample(example);

            resp.setResultCode("001");
            resp.setResultMsg("处理成功");
        } catch (Exception e) {
            LoggerUtil.error(e, "删除会员出现异常 num: " + num);
            resp.setResultCode("003");
            resp.setResultMsg("delete customer by num exception");
        }
        return resp;
    }

    @RequestMapping(value = "addCustomer", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public BaseResp addCustomer(Integer num, String phone, String name, Integer sex, Integer age, Integer preNum) {
        BaseResp resp = new BaseResp();

        try {
            //检查该手机号码是否已经存在
            CustomerExample example = new CustomerExample();
            example.or().andNumEqualTo(num);
            List<Customer> list = customerService.selectByExample(example);
            if (list != null && list.size() > 0) {
                resp.setResultCode("002");
                resp.setResultMsg(StringUtils.getUTF8CharsetString("会员号："+num +"已经存在"));
                return resp;
            }

            Customer customer = new Customer();
            customer.setNum(num);
            customer.setPhone(phone);
            customer.setName(name);
            customer.setSex(sex);
            customer.setAge(age);
            customer.setScore(0.0);

            customerService.insert(customer);

            //更新推荐者的next_num字段
            if (preNum != null) {
                CustomerExample example1 = new CustomerExample();
                example1.or().andNumEqualTo(preNum);

                Customer customer1 = new Customer();
                customer1.setNextNum(num);

                customerService.updateByExampleSelective(customer1, example1);
            }

            resp.setResultCode("001");
            resp.setResultMsg("success");
        } catch (Exception e) {
            resp.setResultCode("003");
            resp.setResultMsg("exception");
            LoggerUtil.error(e, "新增会员出现异常");
        }
        return resp;
    }
}
